package com.atguigu.springcloud.service.impl;

import com.atguigu.springcloud.dao.OrderDao;
import com.atguigu.springcloud.domain.Order;
import com.atguigu.springcloud.service.AccountService;
import com.atguigu.springcloud.service.OrderService;
import com.atguigu.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @USER: dayu
 * @DATE: 2020/4/17
 * @DESCRIPTION:
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private AccountService accountService;
    @Resource
    private StorageService storageService;


    @Override
    @GlobalTransactional(rollbackFor = Exception.class,name = "unique_customer_name")
    public void create(Order order) {

        log.info("----->创建订单");
        orderDao.create(order);

        log.info("----->调用库存，扣减count");
        storageService.delcreate(order.getUserId(),order.getCount());

        log.info("----->调用账户，扣钱");
        accountService.delcreate(order.getUserId(),order.getMoney());

        log.info("----->修改订单状态");
        orderDao.update(order.getUserId(),order.getStatus());
        log.info("----->订单完成");

    }
}
